package kafe;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class FormInputData extends JFrame{

	@SuppressWarnings("unused")
	private JLabel lbkode = new JLabel ("Kode Menu : "),
					lbMenu = new JLabel ("Nama Menu : "),
					lbJenis = new JLabel ("Jenis : "),
					lbHarga = new JLabel ("Harga : "),
					Salah = new JLabel ("Data Belum Berhasil Dimasukkan"),
					Benar = new JLabel ("Data Berhasil Dimasukkan"),
					laKode = new JLabel("Hasil Kode Menu :"),
					laMenu = new JLabel("Nama Menu :"),
					laJenis = new JLabel("Jenis :"),
					laHarga = new JLabel("Harga :");
	
	private JTextField txtKode = new JTextField(),
						txtMenu = new JTextField(),
						txtHarga = new JTextField(),
						txtKode2 = new JTextField(),
						txtMenu2 = new JTextField(),
						txtJenis = new JTextField(),
						txtHarga2 = new JTextField();
	
	private JRadioButton rb1 = new JRadioButton("Minuman", true),
						rb2 = new JRadioButton("Makanan",false);
	
	private JButton btnInput = new JButton("Input"),
					btnCancel = new JButton("Cancel");
	private JPanel panel = new JPanel();
	
	private Connection kon = null;
	private Statement sta = null;
	private ResultSet rse = null;
	
	FormInputData() {
		super("Form Input Data Menu");
		setingKomponenGui();
		loadDriverDb();
		aksiTombolInput();
		aksiTombolCancel();

	}


	private void loadDriverDb() {
		// TODO Auto-generated method stub
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String dbup = "jdbc:mysql://localhost:3306/db_kafe?user=root&password=";
			kon = DriverManager.getConnection(dbup);
		} catch (Exception ex){ex.printStackTrace();}	
	}
	
	private void aksiTombolInput() {
		// TODO Auto-generated method stub
		btnInput.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent ae) {
				// TODO Auto-generated method stub
				try {
					String jenis ="";
					if(rb1.isSelected()){
						jenis = "0";
					}
					else {
						jenis = "1";
					}
					sta = kon.createStatement();
					String sql = "INSERT INTO tbl_menu VALUES" +
					"('"+txtKode.getText()+"','"+txtMenu.getText()+"',"+
					"'"+jenis+"', '"+txtHarga.getText()+"')";
					sta.executeUpdate(sql);
					txtKode.setText("");
					txtMenu.setText("");
					txtHarga.setText("");
					txtKode.requestFocus();
					Benar.setBounds(40,165,200,20);
					
					Statement stat = kon.createStatement();
					String sql2 = "select kode_menu,nama_menu,kode_jenis,harga from tbl_menu";
					ResultSet res = stat.executeQuery(sql2);
					
					String skode = res.getString("kode_menu");
					String snama = res.getString("nama_menu");
					String sjenis = res.getString("jenis");
					String sharga = res.getString("harga");
					String[] data = {skode,snama,sjenis,sharga};
					res.close();
					kon.close();
					sta.close();
				}catch (Exception ex) {
					ex.printStackTrace();
					Salah.setBounds(40,165,200,20);
				}
			}
		});

	}
	
	private void aksiTombolCancel() {
		// TODO Auto-generated method stub
		btnCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				txtKode.setText("");
				txtMenu.setText("");
				txtHarga.setText("");
				txtKode.requestFocus();
			}
		});
	}

	
	private void setingKomponenGui() {
		// TODO Auto-generated method stub
		panel.setLayout(null);
		setSize(400,600);
		lbkode.setBounds(40, 60, 120, 20);
		lbMenu.setBounds(40, 85, 120, 20);
		lbJenis.setBounds(40, 110, 160, 20);
		lbHarga.setBounds(40, 135, 120, 20);
		txtKode.setBounds(160, 60, 100, 20);
		txtMenu.setBounds(160, 85, 160, 20);
		txtHarga.setBounds(160, 135, 100, 20);
		laKode.setBounds(40, 250, 120, 20);
		laMenu.setBounds(40,275,120,20);
		laJenis.setBounds(40, 300, 120, 20);
		laHarga.setBounds(40, 325, 120, 20);
		txtKode2.setBounds(160, 250, 100, 20);
		txtMenu2.setBounds(160, 275, 160, 20);
		txtJenis.setBounds(160, 300, 100, 20);
		txtHarga2.setBounds(160, 325, 100, 20);
		
		ButtonGroup btnGrp = new ButtonGroup();
		rb1.setBounds(160, 110, 85, 20);
		rb2.setBounds(240, 110, 80, 20);
		btnGrp.add(rb1);
		btnGrp.add(rb2);
		btnInput.setBounds(100, 190, 80, 20);
		btnCancel.setBounds(190, 190, 80, 20);
		
		panel.add(Salah);
		panel.add(Benar);
		panel.add(laHarga);
		panel.add(laJenis);
		panel.add(laKode);
		panel.add(laMenu);
		panel.add(txtHarga2);
		panel.add(txtKode2);
		panel.add(txtJenis);
		panel.add(txtMenu2);
		panel.add(lbkode);
		panel.add(lbMenu);
		panel.add(lbJenis);
		panel.add(lbHarga);
		panel.add(txtKode);
		panel.add(txtMenu);
		panel.add(rb1);
		panel.add(rb2);
		panel.add(txtHarga);
		panel.add(btnInput);
		panel.add(btnCancel);
		getContentPane().add(panel);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		FormInputData frameKu = new FormInputData();
	}

	
}
